package com.liang.leetcode.binarytree.exercise;

import com.liang.leetcode.binarytree.entity.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 递归遍历二叉树
 */
public class BiTree01_RecursiveTraversal {

    public static void main(String[] args) {
        TreeNode root = new TreeNode();
        List<Integer> result = new ArrayList<>();
        preorder(root, result);
        System.out.println(result);
    }

    // 前序遍历：根左右
    public static void preorder(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        result.add(root.val);
        preorder(root.left, result);
        preorder(root.right, result);
    }

    // 中序遍历：左根右
    public static void inorder(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        inorder(root.left, result);
        result.add(root.val);
        inorder(root.right, result);
    }

    // 后序遍历：左右根
    public static void postorder(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        postorder(root.left, result);
        postorder(root.right, result);
        result.add(root.val);
    }
}
